+ updateHighlights()
+ }
+
+ private var textView: NSTextView? {
+ return (view as? NSScrollView)?.documentView as? NSTextView
+ }
+
+ private func updateHighlights() {
+ if let textView {
+ if let textStorage = textView.textStorage {
+ textStorage.removeAttribute(
+ .backgroundColor, range: NSRange(location: 0, length: textStorage.length))
+
+ for range in highlightRanges {
+ let nsRange = NSRange(range, in: textStorage.string)
+
+ textStorage.addAttribute(.backgroundColor, value: NSColor.syntax.match, range: nsRange)
+ }
+
+ textView.needsDisplay = true
+
+ }
+ }
+ }
+
+ private func focusOnResult() {
+ if let textView {
+ if let textStorage = textView.textStorage {
+ if selectedRange < highlightRanges.count {
+ let range = highlightRanges[selectedRange]
+ let nsRange = NSRange(range, in: textStorage.string)
+ textView.scrollRangeToVisible(nsRange)
+ textView.selectedRange = nsRange
+ }
+ }
+ }
+ }
+
+ private func setSelectionColor() {
+ guard let textView = self.textView else { return }
+
+ var selectedTextAttributes = textView.selectedTextAttributes
+ selectedTextAttributes[.backgroundColor] = NSColor.yellow.withAlphaComponent(0.3)
+ textView.selectedTextAttributes = selectedTextAttributes